package com.dongdongshop.controller;

import com.alibaba.dubbo.common.serialize.fastjson.FastJsonObjectInput;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.specification_option.TbSpecificationOption;
import com.dongdongshop.service.specification.SpecificationService;
import com.dongdongshop.service.specification_option.SpecificationOptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//规格Controller
@Controller
@RequestMapping("specification")
public class SpecificationController {
    @Reference
    private SpecificationService specificationService;
    @Reference
    private SpecificationOptionService specificationOptionService;
    private final Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    //跳转规格管理页面
    @RequestMapping("SpecificationShow")
    public String toSpecificationShow() {
        return "admin/specification";
    }

    //查询规格并返回JSON
    @RequestMapping("listSpecification")
    @ResponseBody
    private Result listSpecification(TbSpecification tbSpecification) {
        try {
            List<TbSpecification> list = specificationService.listSpecification(tbSpecification.getSpecName());
            return Result.ok().setData(list);
        } catch (Exception e) {
            logger.error("查询错误", e);
            return Result.fail();
        }
    }

    //增加方法
    @RequestMapping("insertSpecification")
    @ResponseBody
    public Result insertSpecification(String json, String specName) {
        try {
            //将json字符串转换成list集合
            List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(json, TbSpecificationOption.class);
            TbSpecification specification = new TbSpecification();
            specification.setSpecName(specName);
            specificationService.insertSpecification(tbSpecificationOptions, specification);
            return Result.ok();
        } catch (Exception e) {
            logger.error("增加失败", e);
            return Result.fail();
        }

    }

    //修改回显
    @RequestMapping("getSpecification")
    @ResponseBody
    public Result getSpecification(Long id) {
        try {
            TbSpecification tbSpecification = specificationService.getSpecification(id);
            List<TbSpecificationOption> tbSpecificationOptionList = specificationOptionService.getSpecificationOption(id);
            tbSpecification.setOtions(tbSpecificationOptionList);
            return Result.ok().setInfo(tbSpecification);
        } catch (Exception e) {
            logger.error("回显失败", e);
            return Result.fail();
        }

    }

    //修改方法
    @RequestMapping("updateSpecification")
    @ResponseBody
    public Result updateSpecification(String json, TbSpecification tbSpecification) {
        try {
            List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(json, TbSpecificationOption.class);
            specificationService.updateSpecification(tbSpecificationOptions, tbSpecification);
            return Result.ok();
        } catch (Exception e) {
            logger.error("修改失败", e);
            return Result.fail();
        }

    }
     //批量删除
    @RequestMapping("deleteSpecificationBatch")
    @ResponseBody
    public Result deleteSpecificationBatch(@RequestParam("ids[]") Long[] ids) {
        try {
            specificationService.deleteSpecificationBatch(ids);
            return Result.ok();
        } catch (Exception e) {
            logger.error("删除失败",e);
            return Result.fail();
        }
    }
}
